How to solve Only Web services with a [ScriptService] attribute on the class definition can be called from script

Posted by NevenHuynh on Stack Overflow See other posts from Stack Overflow or by NevenHuynh
Published on 2011-09-13T18:12:46Z Indexed on 2012/04/04 11:28 UTC
Read the original article Hit count: 195

I attempt to use webservice return POCO class generated from entity data model as JSON when using Jquery AJAX call method in webservice. but I have problem with error "Only Web services with a [ScriptService] attribute on the class definition can be called from script", and getting stuck in it,

Here is my code :

namespace CarCareCenter.Web.Admin.Services
{
    /// <summary>
    /// Summary description for About
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class About : System.Web.Services.WebService
    {
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public static Entities.Category getAbout()
        {
            Entities.Category about = new Entities.Category();
            using (var context = new CarCareCenterDataEntities())
            {
                about = (from c in context.Categories where c.Type == "About" select c).SingleOrDefault();
            }

            return about;
        }
    }
}

aspx page :

<script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                url: '/Services/About.asmx/getAbout',
                data: '{}',
                success: function (response) {
                    var aboutContent = response.d;
                    alert(aboutContent);
                    $('#title-en').val(aboutContent.Name);
                    $('#title-vn').val(aboutContent.NameVn);
                    $('#content-en').val(aboutContent.Description);
                    $('#content-vn').val(aboutContent.DescriptionVn);
                    $('#id').val(aboutContent.CategoryId);
                },
                failure: function (message) {
                    alert(message);
                },
                error: function (result) {
                    alert(result);
                }
            });



            $('#SaveChange').bind('click', function () { updateAbout(); return false; });
            $('#Reset').bind('click', function () { getAbout(); return false; })
        });

        function updateAbout() {
            var abt = {
                "CategoryId": $('#id').val(),
                "Name": $('#title-en').val(),
                "NameVn": $('#title-vn').val(),
                "Description": $('#content-en').val(),
                "DescriptionVn": $('#content-vn').val()
            };
            $.ajax({
                type: "POST",
                url: "AboutManagement.aspx/updateAbout",
                data: JSON.stringify(abt),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var aboutContent = response.d;
                    $('#title-en').val(aboutContent.Name);
                    $('#title-vn').val(aboutContent.NameVn);
                    $('#content-en').val(aboutContent.Description);
                    $('#content-vn').val(aboutContent.DescriptionVn);
                },
                failure: function (message) {
                    alert(message);
                },
                error: function (result) {
                    alert(result);
                }
            });
        }
    </script>

Do any approaches to solve it ? Please help me . Thanks

© Stack Overflow or respective owner

Related posts about web-services

Related posts about error-handling